// components/rollingbox/rollingbox.js
import { axios } from '../../utils/ajax.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    grouplist: {
      type: Array,
      value: []
    },
    rolltype: {
        type: Number,
        value: 0
    },
    isShow: {
      type: Boolean,
      value: false,
      observer: function (newVal, oldVal, changedPath) {
        this.setData({
          isShowdata: newVal
        })
        // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
        // 通常 newVal 就是新设置的数据， oldVal 是旧数据
        this.judge()
      }
    },
    goodsId: {
      type:String,
      value:'',
      observer: function(newVal, oldVal, changedPath) {
        this.setData({
          goodsIddata: newVal
        })
        this.judge()
      }
    }
  },

  detached(){

  },

  moved() {

  },

  /**
   * 组件的初始数据
   */
  data: {
    timer: null,
    isShowdata:'',
    goodsIddata:'',
    newsanimate:false,
    news: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setRolling(){
      axios({
        url: '/home/groupAdvertisement',
        data:{
          goodsId: this.data.goodsId
        }
      }).then((res) => {
        console.log(res.data.grouplist.length)
        this.setData({
          grouplist: res.data.grouplist
        })
        if (this.data.grouplist.length < 1) {
          return;
        }
        this.setData({
          newsanimate: true,
          news: this.data.grouplist[0]
        })
        let newsNum = 0;
        let newsCount = setInterval(() => {
          newsNum = newsNum + 1;
          if (newsNum > this.data.grouplist.length - 1) {
            newsNum = 0;
          }
          this.setData({
            newsanimate: false,
            news: this.data.grouplist[newsNum] ? this.data.grouplist[newsNum] : []
          }, () => {
            this.setData({
              newsanimate: true
            })
          })
        }, 6000)
      })
    },
    judge () {
      if (this.data.isShowdata && this.data.goodsIddata) {
        this.setData({
          rollingtop: 0
        })
        this.setRolling();
      } else {
        clearInterval(this.data.timer);
        this.setData({
          grouplist: []
        })
      }
    }
  },
  ready () {
    
  }
})
